﻿package com.xiaoqee.hadoop.study.hdfs;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

public class HdfsURIMain {

	public static void main(String[] args) throws Exception {

//		read();
//		write();
//		HdfsUtils.readHDFSFile("/user/zengjr/mr/wordcount/input/up-wc.input");
		//大于128M
//		HdfsUtils.writeHDFSFile("/user/zengjr/mr/wordcount/input/bigsize", "d:/abc.ch");
		
//		HdfsUtils.uploadFile("d:/b1.txt", "/user/zengjr/mr/wordcount/input/up-b13.txt");
		
//		HdfsUtils.downloadFile("d:/b5.txt", "/user/zengjr/mr/wordcount/input/up-wc1.input");
		
//		HdfsUtils.reNameFile("/user/zengjr/mr/wordcount/input/up-wc.input", "/user/zengjr/mr/wordcount/input/up-wc1.input");
		
//		HdfsUtils.delFile("/user/zengjr/mr/wordcount/input/up-b11.txt");
		
//		HdfsUtils.mkDir("/user/zengjr/mr/hdfs/a");
//		HdfsUtils.delDir("/user/zengjr/mr/hdfs");
//		
		HdfsUtils.getFileListInfo("/user/zengjr/mr/wordcount");
//		HdfsUtils.getFileInDnPosition("/user/zengjr/mr/wordcount/input/bigsize");
		
//		HdfsUtils.getDNInfo();
		
//		HdfsUtils.createFileAndWriteStr("/user/zengjr/mr/wordcount/input/up-b15.txt","你好,中国，chinese");
	}

	//重复上传同名文件会覆盖
	public static void write() throws IOException, URISyntaxException,
			InterruptedException {
		
		String uri = "/user/zengjr/mr/wordcount/input/up-wc.input";

		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://hadoop-zengjr.xiaoqee.com:8020");

		FileSystem fileSystem = FileSystem.get(new URI(uri), conf);

		// 第三个用户名，加上权限验证，需要在配置文件中开启dfs.permissions-->true
//		fileSystem = FileSystem.get(new URI(uri), conf, "zengjr");

		// FileSystem.newInstance(conf);
		// 取本地文件系统
		// LocalFileSystem lfs = FileSystem.getLocal(conf);
		
		FSDataOutputStream outStream = fileSystem.create(new Path(uri));
		
	
		FileInputStream inStream = new FileInputStream(new File("D:/b.txt"));

		try {
			IOUtils.copyBytes(inStream, outStream, 4096, false);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeStream(inStream);
			IOUtils.closeStream(outStream);
			
		}
	}

	public static void read() throws IOException, URISyntaxException,
			InterruptedException {
		String uri = "/user/zengjr/mr/wordcount/input/up-wc.input";

		Configuration conf = new Configuration();
		conf.set("fs.defaultFS", "hdfs://hadoop-zengjr.xiaoqee.com:8020");

		FileSystem fileSystem = FileSystem.get(new URI(uri), conf);

		// 第三个用户名，加上权限验证，需要在配置文件中开启dfs.permissions-->true
//		fileSystem = FileSystem.get(new URI(uri), conf, "zengjr");

		// FileSystem.newInstance(conf);
		// 取本地文件系统
		// LocalFileSystem lfs = FileSystem.getLocal(conf);

		FSDataInputStream inStream = fileSystem.open(new Path(uri));

		try {
			IOUtils.copyBytes(inStream, System.out, 4096, false);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeStream(inStream);
		}
	}

}
